﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace WeiMiao { 
    public class Music : MonoBehaviour
    {
        //成功
        public AudioClip[] musicfile;

        public bool[] trigger;

        public AudioSource audiosource1;
        public AudioSource audiosource2;

        public GameObject volumestrip, musicbtn, soundbtn;
        public bool musicsilence, soundsilence;
        public float volume;

        // Start is called before the first frame update
        void Start()
        {
            DontDestroyOnLoad(gameObject);
            //获取设置
            if (!PlayerPrefs.HasKey("music") || !PlayerPrefs.HasKey("sound") || !PlayerPrefs.HasKey("volume"))
            {
                Debug.Log("不存在");
                PlayerPrefs.SetInt("music", 1);

                PlayerPrefs.SetInt("sound", 1);

                PlayerPrefs.SetFloat("volume", 1);
            }
            volumestrip.GetComponent<Slider>().value = PlayerPrefs.GetFloat("volume");
            musicbtn.GetComponent<Toggle>().isOn = Convert.ToBoolean(PlayerPrefs.GetInt("music"));
            soundbtn.GetComponent<Toggle>().isOn = Convert.ToBoolean(PlayerPrefs.GetInt("sound"));
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="number">第几个（从0开始）</param>
        public void Sound(int number)
        {
            audiosource2.clip = musicfile[number];
            audiosource2.Play();
        }

        //PlayerPrefs用于储存
        void Update()
        {
            if(musicsilence != musicbtn.GetComponent<Toggle>().isOn)
            {
                musicsilence = musicbtn.GetComponent<Toggle>().isOn;
                if (!musicsilence)
                {
                    audiosource1.volume = 0;
                    PlayerPrefs.SetInt("music", 0);
                }
                else
                {
                    PlayerPrefs.SetInt("music", 1);
                    audiosource1.volume = volumestrip.GetComponent<Slider>().value;
                }
            }
            if(soundsilence != soundbtn.GetComponent<Toggle>().isOn)
            {
                soundsilence = soundbtn.GetComponent<Toggle>().isOn;
                if (!soundsilence)
                {
                    audiosource2.volume = 0;
                    PlayerPrefs.SetInt("sound", 0);
                }
                else
                {
                    audiosource2.volume = volumestrip.GetComponent<Slider>().value;
                    PlayerPrefs.SetInt("sound", 1);
                }
            }
            if(volume != volumestrip.GetComponent<Slider>().value)
            {
                if (musicbtn.GetComponent<Toggle>().isOn) 
                { 
                    audiosource1.volume = volumestrip.GetComponent<Slider>().value;
                }
                else
                {
                    audiosource1.volume = 0;
                }
                if (soundbtn.GetComponent<Toggle>().isOn)
                {
                    audiosource2.volume = volumestrip.GetComponent<Slider>().value;
                }
                else
                {
                    audiosource2.volume = 0;
                }
                volume = volumestrip.GetComponent<Slider>().value;
                PlayerPrefs.SetFloat("volume", volumestrip.GetComponent<Slider>().value);
            }
        }
    }
}